package com.hqu.ms.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hqu.ms.utils.JSONResponse.Status;

import android.util.Log;


public class JSONUtil {

	private final static String BASE_URL = "http://www.fangchemei.cn/phpport/glemeiInterFace/mainInterface.php";
	private final static int STATUS_CODE_OK = 200;
	private static HttpClient httpClient = null;
	private static Header[] headers = { new BasicHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; " + "Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)"), new BasicHeader("Accept-Language", "zh-cn"),
			new BasicHeader("Accept", " image/gif, image/x-xbitmap, image/jpeg, " + "image/pjpeg, application/x-silverlight, application/vnd.ms-excel, " + "application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"),
			new BasicHeader("Content-Type", "application/x-www-form-urlencoded"),
			new BasicHeader("enctype", "multipart/form-data"),
			new BasicHeader("Accept-Encoding", "gzip, deflate") };
	static {
		httpClient = new DefaultHttpClient();

	}

	public static JSONResponse postRequest(int way, JSONObject data) {
		Log.d("data", data.toString());
		HttpPost post = new HttpPost(BASE_URL);
		post.setHeaders(headers);
		List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
		nameValuePairList.add(new BasicNameValuePair("way", Integer.toString(way)));
		nameValuePairList.add(new BasicNameValuePair("jsondata", data.toString()));
		JSONResponse response = new JSONResponse();
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8");
			Log.v(JSONUtil.class.toString(), "send request ->" + EntityUtils.toString(entity, "UTF-8"));
			post.setEntity(entity);
			HttpResponse httpResponse = httpClient.execute(post);
			if (httpResponse.getStatusLine().getStatusCode() == STATUS_CODE_OK) {
				String retSrc = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
				Log.v(JSONUtil.class.toString(), "response ->" + retSrc);
				response.setStatus(Status.OK);
				response.setData(new JSONObject(retSrc));
			}
		} catch (Exception e) {
			response.setStatus(Status.ERR);
		}
		return response;
	}

	public static JSONObject getJSONObject(List<NameValuePair> nameValuePairList) {
		JSONObject jsonObject = new JSONObject();
		try {
			for (NameValuePair nameValuePair : nameValuePairList) {
				jsonObject.put(nameValuePair.getName(), nameValuePair.getValue());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	public static JSONObject getJSONObject(String name, Object value) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put(name, value);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	public static String[] JSONStringArrayToStringArray(JSONArray jsonArray) {
		String[] result = new String[jsonArray.length()];
		try {
			for (int i = 0; i < jsonArray.length(); i++) {
				result[i] = jsonArray.getString(i);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}
}
